"""
实时性能监控Dashboard和关键指标告警
提供系统性能监控、指标收集、可视化和告警功能
"""
import asyncio
import json
import time
import threading
from typing import Any, Dict, List, Optional, Callable, Union
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from collections import defaultdict, deque
from enum import Enum
import statistics
import psutil
import aiohttp
from aiohttp import web
import aiofiles
from loguru import logger

from utils.logger import Logger
# 创建一个简单的配置类来替代不存在的utils.config
class Config:
    def __init__(self):
        self._config = {
            "monitoring.retention_hours": 24,
            "monitoring.system.enabled": True,
            "monitoring.custom.enabled": True,
            "monitoring.collect_interval": 30
        }
    
    def get(self, key, default=None):
        return self._config.get(key, default)
from utils.exceptions import MaessBaseException as MonitoringException, ErrorCode
from core.tracing.distributed_tracing import get_distributed_tracer


class AlertLevel(Enum):
    """告警级别"""
    INFO = "info"
    WARNING = "warning"
    CRITICAL = "critical"
    EMERGENCY = "emergency"


class MetricType(Enum):
    """指标类型"""
    COUNTER = "counter"
    GAUGE = "gauge"
    HISTOGRAM = "histogram"
    SUMMARY = "summary"


@dataclass
class MetricData:
    """指标数据"""
    name: str
    metric_type: MetricType
    value: Union[int, float]
    timestamp: datetime
    labels: Dict[str, str] = field(default_factory=dict)
    description: str = ""
    unit: str = ""


@dataclass
class AlertRule:
    """告警规则"""
    name: str
    metric_name: str
    condition: str  # ">", "<", "==", "!=", ">=", "<="
    threshold: Union[int, float]
    duration: int  # 持续时间（秒）
    level: AlertLevel
    description: str = ""
    enabled: bool = True
    cooldown: int = 300  # 告警冷却时间（秒）
    labels: Dict[str, str] = field(default_factory=dict)


@dataclass
class AlertEvent:
    """告警事件"""
    rule_name: str
    level: AlertLevel
    message: str
    metric_name: str
    current_value: Union[int, float]
    threshold: Union[int, float]
    timestamp: datetime
    acknowledged: bool = False
    resolved: bool = False
    resolved_at: Optional[datetime] = None


class MetricsCollector:
    """指标收集器"""
    
    def __init__(self, config: Config):
        self.config = config
        self.logger = Logger.get_logger(agent_name="metrics_collector")
        
        # 指标存储
        self.metrics: Dict[str, List[MetricData]] = defaultdict(list)
        self.metric_history: Dict[str, deque] = defaultdict(
            lambda: deque(maxlen=config.get("monitoring.retention_hours", 24) * 60)
        )
        
        # 系统指标
        self.system_metrics_enabled = config.get("monitoring.system.enabled", True)
        self.custom_metrics_enabled = config.get("monitoring.custom.enabled", True)
        
        # 收集间隔
        self.collect_interval = config.get("monitoring.collect_interval", 30)  # 30秒
        
        # 运行状态
        self.running = False
        self.collect_task: Optional[asyncio.Task] = None
        
        self.logger.info("指标收集器初始化完成")
    
    async def start(self):
        """启动指标收集"""
        if self.running:
            return
        
        self.running = True
        self.collect_task = asyncio.create_task(self._collect_loop())
        self.logger.info("指标收集器已启动")
    
    async def stop(self):
        """停止指标收集"""
        if not self.running:
            return
        
        self.running = False
        if self.collect_task:
            self.collect_task.cancel()
            try:
                await self.collect_task
            except asyncio.CancelledError:
                pass
        
        self.logger.info("指标收集器已停止")
    
    async def _collect_loop(self):
        """指标收集循环"""
        while self.running:
            try:
                await self._collect_metrics()
                await asyncio.sleep(self.collect_interval)
                
            except asyncio.CancelledError:
                break
            except Exception as e:
                self.logger.error(f"指标收集循环错误: {str(e)}")
                await asyncio.sleep(self.collect_interval)
    
    async def _collect_metrics(self):
        """收集指标"""
        try:
            timestamp = datetime.now()
            
            # 收集系统指标
            if self.system_metrics_enabled:
                await self._collect_system_metrics(timestamp)
            
            # 收集自定义指标
            if self.custom_metrics_enabled:
                await self._collect_custom_metrics(timestamp)
            
            # 清理过期数据
            self._cleanup_old_metrics()
            
        except Exception as e:
            self.logger.error(f"收集指标失败: {str(e)}")
    
    async def _collect_system_metrics(self, timestamp: datetime):
        """收集系统指标"""
        try:
            # CPU使用率
            cpu_percent = psutil.cpu_percent(interval=1)
            self._add_metric("system.cpu.usage", MetricType.GAUGE, cpu_percent, timestamp, 
                           {"unit": "percent", "description": "CPU使用率"})
            
            # 内存使用率
            memory = psutil.virtual_memory()
            self._add_metric("system.memory.usage", MetricType.GAUGE, memory.percent, timestamp,
                           {"unit": "percent", "description": "内存使用率"})
            self._add_metric("system.memory.total", MetricType.GAUGE, memory.total, timestamp,
                           {"unit": "bytes", "description": "总内存"})
            self._add_metric("system.memory.available", MetricType.GAUGE, memory.available, timestamp,
                           {"unit": "bytes", "description": "可用内存"})
            
            # 磁盘使用率
            disk = psutil.disk_usage('/')
            self._add_metric("system.disk.usage", MetricType.GAUGE, disk.percent, timestamp,
                           {"unit": "percent", "description": "磁盘使用率"})
            self._add_metric("system.disk.total", MetricType.GAUGE, disk.total, timestamp,
                           {"unit": "bytes", "description": "总磁盘空间"})
            self._add_metric("system.disk.free", MetricType.GAUGE, disk.free, timestamp,
                           {"unit": "bytes", "description": "可用磁盘空间"})
            
            # 网络IO
            net_io = psutil.net_io_counters()
            self._add_metric("system.network.bytes_sent", MetricType.COUNTER, net_io.bytes_sent, timestamp,
                           {"unit": "bytes", "description": "网络发送字节数"})
            self._add_metric("system.network.bytes_recv", MetricType.COUNTER, net_io.bytes_recv, timestamp,
                           {"unit": "bytes", "description": "网络接收字节数"})
            
            # 进程信息
            process = psutil.Process()
            self._add_metric("system.process.cpu_percent", MetricType.GAUGE, process.cpu_percent(), timestamp,
                           {"unit": "percent", "description": "进程CPU使用率"})
            self._add_metric("system.process.memory_rss", MetricType.GAUGE, process.memory_info().rss, timestamp,
                           {"unit": "bytes", "description": "进程内存RSS"})
            self._add_metric("system.process.threads", MetricType.GAUGE, process.num_threads(), timestamp,
                           {"unit": "count", "description": "进程线程数"})
            
        except Exception as e:
            self.logger.error(f"收集系统指标失败: {str(e)}")
    
    async def _collect_custom_metrics(self, timestamp: datetime):
        """收集自定义指标"""
        try:
            # 从追踪器获取指标
            tracer = get_distributed_tracer()
            
            # 请求计数
            request_count = getattr(tracer, 'request_count', 0)
            self._add_metric("app.requests.total", MetricType.COUNTER, request_count, timestamp,
                           {"unit": "count", "description": "总请求数"})
            
            # 错误计数
            error_count = getattr(tracer, 'error_count', 0)
            self._add_metric("app.errors.total", MetricType.COUNTER, error_count, timestamp,
                           {"unit": "count", "description": "总错误数"})
            
            # 平均响应时间
            avg_response_time = getattr(tracer, 'avg_response_time', 0)
            self._add_metric("app.response_time.avg", MetricType.GAUGE, avg_response_time, timestamp,
                           {"unit": "ms", "description": "平均响应时间"})
            
            # 缓存命中率
            cache_hit_rate = getattr(tracer, 'cache_hit_rate', 0)
            self._add_metric("app.cache.hit_rate", MetricType.GAUGE, cache_hit_rate, timestamp,
                           {"unit": "percent", "description": "缓存命中率"})
            
            # 文档处理指标
            doc_processed = getattr(tracer, 'docs_processed', 0)
            self._add_metric("app.docs.processed", MetricType.COUNTER, doc_processed, timestamp,
                           {"unit": "count", "description": "处理的文档数"})
            
            # 分片指标
            segments_created = getattr(tracer, 'segments_created', 0)
            self._add_metric("app.segments.created", MetricType.COUNTER, segments_created, timestamp,
                           {"unit": "count", "description": "创建的分片数"})
            
        except Exception as e:
            self.logger.error(f"收集自定义指标失败: {str(e)}")
    
    def _add_metric(self, name: str, metric_type: MetricType, value: Union[int, float],
                   timestamp: datetime, metadata: Dict[str, Any] = None):
        """添加指标"""
        try:
            metric = MetricData(
                name=name,
                metric_type=metric_type,
                value=value,
                timestamp=timestamp,
                description=metadata.get("description", "") if metadata else "",
                unit=metadata.get("unit", "") if metadata else ""
            )
            
            # 添加到当前指标列表
            self.metrics[name].append(metric)
            
            # 添加到历史数据
            self.metric_history[name].append({
                "timestamp": timestamp.isoformat(),
                "value": value
            })
            
        except Exception as e:
            self.logger.error(f"添加指标失败: {name}, 错误: {str(e)}")
    
    def _cleanup_old_metrics(self):
        """清理过期指标"""
        try:
            cutoff_time = datetime.now() - timedelta(hours=self.config.get("monitoring.retention_hours", 24))
            
            for name, metrics_list in self.metrics.items():
                self.metrics[name] = [
                    metric for metric in metrics_list
                    if metric.timestamp > cutoff_time
                ]
            
        except Exception as e:
            self.logger.error(f"清理过期指标失败: {str(e)}")
    
    def get_metrics(self, name: Optional[str] = None, 
                   start_time: Optional[datetime] = None,
                   end_time: Optional[datetime] = None) -> List[MetricData]:
        """获取指标数据"""
        try:
            if name:
                metrics_list = self.metrics.get(name, [])
            else:
                # 获取所有指标
                metrics_list = []
                for metrics_list_by_name in self.metrics.values():
                    metrics_list.extend(metrics_list_by_name)
            
            # 过滤时间范围
            if start_time:
                metrics_list = [m for m in metrics_list if m.timestamp >= start_time]
            if end_time:
                metrics_list = [m for m in metrics_list if m.timestamp <= end_time]
            
            return metrics_list
            
        except Exception as e:
            self.logger.error(f"获取指标失败: {str(e)}")
            return []
    
    def get_metric_history(self, name: str, hours: int = 24) -> List[Dict[str, Any]]:
        """获取指标历史数据"""
        try:
            history = list(self.metric_history.get(name, []))
            
            # 过滤时间范围
            cutoff_time = datetime.now() - timedelta(hours=hours)
            filtered_history = [
                item for item in history
                if datetime.fromisoformat(item["timestamp"]) > cutoff_time
            ]
            
            return filtered_history
            
        except Exception as e:
            self.logger.error(f"获取指标历史失败: {str(e)}")
            return []


class AlertManager:
    """告警管理器"""
    
    def __init__(self, config: Config):
        self.config = config
        self.logger = Logger.get_logger(agent_name="alert_manager")
        
        # 告警规则
        self.rules: Dict[str, AlertRule] = {}
        self.alert_events: List[AlertEvent] = []
        
        # 告警状态
        self.alert_states: Dict[str, Dict[str, Any]] = defaultdict(dict)
        
        # 通知配置
        self.notification_enabled = config.get("monitoring.alerts.enabled", True)
        self.notification_channels = config.get("monitoring.alerts.channels", ["webhook"])
        
        # 运行状态
        self.running = False
        self.check_task: Optional[asyncio.Task] = None
        self.check_interval = config.get("monitoring.alerts.check_interval", 60)  # 60秒
        
        # 初始化默认告警规则
        self._initialize_default_rules()
        
        self.logger.info("告警管理器初始化完成")
    
    def _initialize_default_rules(self):
        """初始化默认告警规则"""
        try:
            default_rules = [
                AlertRule(
                    name="high_cpu_usage",
                    metric_name="system.cpu.usage",
                    condition=">",
                    threshold=80,
                    duration=300,  # 5分钟
                    level=AlertLevel.WARNING,
                    description="CPU使用率过高",
                    cooldown=600  # 10分钟冷却
                ),
                AlertRule(
                    name="high_memory_usage",
                    metric_name="system.memory.usage",
                    condition=">",
                    threshold=85,
                    duration=300,
                    level=AlertLevel.WARNING,
                    description="内存使用率过高",
                    cooldown=600
                ),
                AlertRule(
                    name="high_disk_usage",
                    metric_name="system.disk.usage",
                    condition=">",
                    threshold=90,
                    duration=600,
                    level=AlertLevel.CRITICAL,
                    description="磁盘使用率过高",
                    cooldown=3600
                ),
                AlertRule(
                    name="high_error_rate",
                    metric_name="app.errors.total",
                    condition=">",
                    threshold=100,  # 100个错误
                    duration=300,
                    level=AlertLevel.WARNING,
                    description="错误率过高",
                    cooldown=300
                ),
                AlertRule(
                    name="high_response_time",
                    metric_name="app.response_time.avg",
                    condition=">",
                    threshold=5000,  # 5秒
                    duration=300,
                    level=AlertLevel.WARNING,
                    description="响应时间过长",
                    cooldown=300
                ),
                AlertRule(
                    name="low_cache_hit_rate",
                    metric_name="app.cache.hit_rate",
                    condition="<",
                    threshold=50,  # 50%
                    duration=600,
                    level=AlertLevel.INFO,
                    description="缓存命中率过低",
                    cooldown=1800
                )
            ]
            
            for rule in default_rules:
                self.add_rule(rule)
            
            self.logger.info(f"初始化 {len(default_rules)} 个默认告警规则")
            
        except Exception as e:
            self.logger.error(f"初始化默认告警规则失败: {str(e)}")
    
    def add_rule(self, rule: AlertRule):
        """添加告警规则"""
        try:
            self.rules[rule.name] = rule
            self.logger.info(f"添加告警规则: {rule.name}")
            
        except Exception as e:
            self.logger.error(f"添加告警规则失败: {str(e)}")
    
    def remove_rule(self, rule_name: str):
        """删除告警规则"""
        try:
            if rule_name in self.rules:
                del self.rules[rule_name]
                self.logger.info(f"删除告警规则: {rule_name}")
            
        except Exception as e:
            self.logger.error(f"删除告警规则失败: {str(e)}")
    
    async def start(self):
        """启动告警管理器"""
        if self.running:
            return
        
        self.running = True
        self.check_task = asyncio.create_task(self._check_loop())
        self.logger.info("告警管理器已启动")
    
    async def stop(self):
        """停止告警管理器"""
        if not self.running:
            return
        
        self.running = False
        if self.check_task:
            self.check_task.cancel()
            try:
                await self.check_task
            except asyncio.CancelledError:
                pass
        
        self.logger.info("告警管理器已停止")
    
    async def _check_loop(self):
        """告警检查循环"""
        while self.running:
            try:
                await self._check_alerts()
                await asyncio.sleep(self.check_interval)
                
            except asyncio.CancelledError:
                break
            except Exception as e:
                self.logger.error(f"告警检查循环错误: {str(e)}")
                await asyncio.sleep(self.check_interval)
    
    async def _check_alerts(self):
        """检查告警"""
        try:
            from core.monitoring.performance_monitor import get_metrics_collector
            
            metrics_collector = get_metrics_collector()
            current_time = datetime.now()
            
            for rule_name, rule in self.rules.items():
                if not rule.enabled:
                    continue
                
                # 检查冷却时间
                last_alert = self.alert_states.get(rule_name, {}).get("last_alert_time")
                if last_alert and (current_time - last_alert).seconds < rule.cooldown:
                    continue
                
                # 获取指标数据
                metrics_data = metrics_collector.get_metrics(
                    rule.metric_name,
                    current_time - timedelta(seconds=rule.duration),
                    current_time
                )
                
                if not metrics_data:
                    continue
                
                # 计算指标值（取平均值）
                values = [m.value for m in metrics_data]
                current_value = statistics.mean(values) if values else 0
                
                # 检查条件
                condition_met = self._evaluate_condition(
                    current_value, rule.condition, rule.threshold
                )
                
                if condition_met:
                    # 检查持续时间
                    duration_met = self._check_duration(rule_name, condition_met, rule.duration)
                    
                    if duration_met:
                        # 触发告警
                        await self._trigger_alert(rule, current_value, current_time)
                        
                        # 更新告警状态
                        self.alert_states[rule_name]["last_alert_time"] = current_time
                        self.alert_states[rule_name]["last_value"] = current_value
                
                else:
                    # 重置持续时间检查
                    self.alert_states[rule_name]["condition_start_time"] = None
        
        except Exception as e:
            self.logger.error(f"检查告警失败: {str(e)}")
    
    def _evaluate_condition(self, value: Union[int, float], condition: str, threshold: Union[int, float]) -> bool:
        """评估条件"""
        try:
            if condition == ">":
                return value > threshold
            elif condition == "<":
                return value < threshold
            elif condition == "==":
                return value == threshold
            elif condition == "!=":
                return value != threshold
            elif condition == ">=":
                return value >= threshold
            elif condition == "<=":
                return value <= threshold
            else:
                self.logger.warning(f"未知的条件类型: {condition}")
                return False
                
        except Exception as e:
            self.logger.error(f"评估条件失败: {str(e)}")
            return False
    
    def _check_duration(self, rule_name: str, condition_met: bool, duration: int) -> bool:
        """检查持续时间"""
        try:
            current_time = datetime.now()
            condition_start = self.alert_states[rule_name].get("condition_start_time")
            
            if condition_met:
                if not condition_start:
                    self.alert_states[rule_name]["condition_start_time"] = current_time
                    return False
                else:
                    duration_seconds = (current_time - condition_start).seconds
                    return duration_seconds >= duration
            else:
                self.alert_states[rule_name]["condition_start_time"] = None
                return False
                
        except Exception as e:
            self.logger.error(f"检查持续时间失败: {str(e)}")
            return False
    
    async def _trigger_alert(self, rule: AlertRule, current_value: Union[int, float], timestamp: datetime):
        """触发告警"""
        try:
            # 创建告警事件
            alert_event = AlertEvent(
                rule_name=rule.name,
                level=rule.level,
                message=f"{rule.description}: 当前值 {current_value:.2f} {rule.condition} {rule.threshold}",
                metric_name=rule.metric_name,
                current_value=current_value,
                threshold=rule.threshold,
                timestamp=timestamp
            )
            
            # 添加到告警事件列表
            self.alert_events.append(alert_event)
            
            # 限制告警事件数量
            max_events = self.config.get("monitoring.alerts.max_events", 1000)
            if len(self.alert_events) > max_events:
                self.alert_events = self.alert_events[-max_events:]
            
            # 发送通知
            if self.notification_enabled:
                await self._send_notification(alert_event)
            
            self.logger.warning(f"告警触发: {rule.name} - {alert_event.message}")
            
        except Exception as e:
            self.logger.error(f"触发告警失败: {str(e)}")
    
    async def _send_notification(self, alert_event: AlertEvent):
        """发送通知"""
        try:
            for channel in self.notification_channels:
                if channel == "webhook":
                    await self._send_webhook_notification(alert_event)
                elif channel == "email":
                    await self._send_email_notification(alert_event)
                elif channel == "sms":
                    await self._send_sms_notification(alert_event)
                else:
                    self.logger.warning(f"未知的通知渠道: {channel}")
        
        except Exception as e:
            self.logger.error(f"发送通知失败: {str(e)}")
    
    async def _send_webhook_notification(self, alert_event: AlertEvent):
        """发送Webhook通知"""
        try:
            webhook_url = self.config.get("monitoring.alerts.webhook_url")
            if not webhook_url:
                return
            
            payload = {
                "rule_name": alert_event.rule_name,
                "level": alert_event.level.value,
                "message": alert_event.message,
                "metric_name": alert_event.metric_name,
                "current_value": alert_event.current_value,
                "threshold": alert_event.threshold,
                "timestamp": alert_event.timestamp.isoformat()
            }
            
            async with aiohttp.ClientSession() as session:
                async with session.post(webhook_url, json=payload) as response:
                    if response.status != 200:
                        self.logger.error(f"Webhook通知发送失败: {response.status}")
                    else:
                        self.logger.info(f"Webhook通知发送成功: {alert_event.rule_name}")
        
        except Exception as e:
            self.logger.error(f"发送Webhook通知失败: {str(e)}")
    
    async def _send_email_notification(self, alert_event: AlertEvent):
        """发送邮件通知（简化版）"""
        # 这里可以集成邮件发送服务
        self.logger.info(f"邮件通知（待实现）: {alert_event.rule_name}")
    
    async def _send_sms_notification(self, alert_event: AlertEvent):
        """发送短信通知（简化版）"""
        # 这里可以集成短信发送服务
        self.logger.info(f"短信通知（待实现）: {alert_event.rule_name}")
    
    def get_alerts(self, level: Optional[AlertLevel] = None,
                  acknowledged: Optional[bool] = None,
                  resolved: Optional[bool] = None,
                  limit: int = 100) -> List[AlertEvent]:
        """获取告警事件"""
        try:
            alerts = self.alert_events
            
            # 过滤级别
            if level:
                alerts = [a for a in alerts if a.level == level]
            
            # 过滤确认状态
            if acknowledged is not None:
                alerts = [a for a in alerts if a.acknowledged == acknowledged]
            
            # 过滤解决状态
            if resolved is not None:
                alerts = [a for a in alerts if a.resolved == resolved]
            
            # 限制数量
            return alerts[-limit:]
            
        except Exception as e:
            self.logger.error(f"获取告警失败: {str(e)}")
            return []
    
    def acknowledge_alert(self, alert_index: int):
        """确认告警"""
        try:
            if 0 <= alert_index < len(self.alert_events):
                self.alert_events[alert_index].acknowledged = True
                self.logger.info(f"告警已确认: {self.alert_events[alert_index].rule_name}")
        
        except Exception as e:
            self.logger.error(f"确认告警失败: {str(e)}")
    
    def resolve_alert(self, alert_index: int):
        """解决告警"""
        try:
            if 0 <= alert_index < len(self.alert_events):
                alert = self.alert_events[alert_index]
                alert.resolved = True
                alert.resolved_at = datetime.now()
                self.logger.info(f"告警已解决: {alert.rule_name}")
        
        except Exception as e:
            self.logger.error(f"解决告警失败: {str(e)}")


class MonitoringDashboard:
    """监控Dashboard"""
    
    def __init__(self, config: Config, metrics_collector: MetricsCollector, alert_manager: AlertManager):
        self.config = config
        self.metrics_collector = metrics_collector
        self.alert_manager = alert_manager
        self.logger = Logger.get_logger(agent_name="monitoring_dashboard")
        
        # Dashboard配置
        self.port = config.get("monitoring.dashboard.port", 8080)
        self.host = config.get("monitoring.dashboard.host", "0.0.0.0")
        
        # Web应用
        self.app = web.Application()
        self.runner = None
        self.site = None
        
        # 运行状态
        self.running = False
        
        # 初始化路由
        self._setup_routes()
        
        self.logger.info("监控Dashboard初始化完成")
    
    def _setup_routes(self):
        """设置路由"""
        self.app.router.add_get('/', self.index_handler)
        self.app.router.add_get('/api/metrics', self.metrics_handler)
        self.app.router.add_get('/api/metrics/{metric_name}/history', self.metric_history_handler)
        self.app.router.add_get('/api/alerts', self.alerts_handler)
        self.app.router.add_post('/api/alerts/{alert_index}/acknowledge', self.acknowledge_alert_handler)
        self.app.router.add_post('/api/alerts/{alert_index}/resolve', self.resolve_alert_handler)
        self.app.router.add_get('/api/system/status', self.system_status_handler)
        self.app.router.add_static('/static', 'static')
    
    async def index_handler(self, request):
        """主页处理器"""
        html_content = """
        <!DOCTYPE html>
        <html>
        <head>
            <title>MAESS-Flow 监控Dashboard</title>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
            <style>
                body { font-family: Arial, sans-serif; margin: 20px; }
                .container { max-width: 1200px; margin: 0 auto; }
                .metric-card { background: #f5f5f5; padding: 15px; margin: 10px 0; border-radius: 5px; }
                .alert-critical { background: #ffebee; border-left: 4px solid #f44336; }
                .alert-warning { background: #fff3e0; border-left: 4px solid #ff9800; }
                .alert-info { background: #e3f2fd; border-left: 4px solid #2196f3; }
                .chart-container { width: 100%; height: 300px; margin: 20px 0; }
                .status-ok { color: #4caf50; }
                .status-warning { color: #ff9800; }
                .status-error { color: #f44336; }
            </style>
        </head>
        <body>
            <div class="container">
                <h1>MAESS-Flow 监控Dashboard</h1>
                <div id="system-status"></div>
                <div id="metrics-overview"></div>
                <div id="alerts-section"></div>
                <div id="charts-section"></div>
            </div>
            <script>
                async function loadData() {
                    try {
                        // 加载系统状态
                        const statusResponse = await fetch('/api/system/status');
                        const statusData = await statusResponse.json();
                        updateSystemStatus(statusData);
                        
                        // 加载指标
                        const metricsResponse = await fetch('/api/metrics');
                        const metricsData = await metricsResponse.json();
                        updateMetricsOverview(metricsData);
                        
                        // 加载告警
                        const alertsResponse = await fetch('/api/alerts');
                        const alertsData = await alertsResponse.json();
                        updateAlertsSection(alertsData);
                        
                        // 加载图表数据
                        await loadCharts();
                        
                    } catch (error) {
                        console.error('加载数据失败:', error);
                    }
                }
                
                function updateSystemStatus(data) {
                    const statusDiv = document.getElementById('system-status');
                    statusDiv.innerHTML = `
                        <div class="metric-card">
                            <h3>系统状态</h3>
                            <p>运行时间: ${data.uptime}</p>
                            <p>CPU使用率: <span class="${data.cpu_status}">${data.cpu_usage}%</span></p>
                            <p>内存使用率: <span class="${data.memory_status}">${data.memory_usage}%</span></p>
                            <p>磁盘使用率: <span class="${data.disk_status}">${data.disk_usage}%</span></p>
                        </div>
                    `;
                }
                
                function updateMetricsOverview(data) {
                    const metricsDiv = document.getElementById('metrics-overview');
                    let html = '<div class="metric-card"><h3>关键指标</h3>';
                    
                    data.forEach(metric => {
                        html += `
                            <div style="display: inline-block; margin: 10px; padding: 10px; background: white; border-radius: 3px;">
                                <strong>${metric.name}</strong><br>
                                ${metric.value} ${metric.unit}
                            </div>
                        `;
                    });
                    
                    html += '</div>';
                    metricsDiv.innerHTML = html;
                }
                
                function updateAlertsSection(data) {
                    const alertsDiv = document.getElementById('alerts-section');
                    let html = '<div class="metric-card"><h3>告警信息</h3>';
                    
                    if (data.length === 0) {
                        html += '<p>暂无告警</p>';
                    } else {
                        data.forEach((alert, index) => {
                            const alertClass = alert.level === 'critical' ? 'alert-critical' : 
                                             alert.level === 'warning' ? 'alert-warning' : 'alert-info';
                            html += `
                                <div class="${alertClass}" style="margin: 10px 0; padding: 10px;">
                                    <strong>${alert.rule_name}</strong> - ${alert.level}<br>
                                    ${alert.message}<br>
                                    <small>${new Date(alert.timestamp).toLocaleString()}</small>
                                    ${!alert.acknowledged ? 
                                        `<button onclick="acknowledgeAlert(${index})">确认</button>` : 
                                        '<span style="color: green;">已确认</span>'}
                                </div>
                            `;
                        });
                    }
                    
                    html += '</div>';
                    alertsDiv.innerHTML = html;
                }
                
                async function loadCharts() {
                    // 这里可以加载更详细的图表数据
                    const chartsDiv = document.getElementById('charts-section');
                    chartsDiv.innerHTML = '<div class="metric-card"><h3>性能图表</h3><p>图表加载中...</p></div>';
                }
                
                async function acknowledgeAlert(index) {
                    try {
                        const response = await fetch(`/api/alerts/${index}/acknowledge`, {
                            method: 'POST'
                        });
                        if (response.ok) {
                            loadData(); // 重新加载数据
                        }
                    } catch (error) {
                        console.error('确认告警失败:', error);
                    }
                }
                
                // 定时刷新数据
                setInterval(loadData, 30000); // 30秒刷新一次
                
                // 初始加载
                loadData();
            </script>
        </body>
        </html>
        """
        return web.Response(text=html_content, content_type='text/html')
    
    async def metrics_handler(self, request):
        """指标处理器"""
        try:
            # 获取关键指标
            key_metrics = [
                {"name": "CPU使用率", "value": 0, "unit": "%"},
                {"name": "内存使用率", "value": 0, "unit": "%"},
                {"name": "磁盘使用率", "value": 0, "unit": "%"},
                {"name": "请求总数", "value": 0, "unit": "次"},
                {"name": "错误总数", "value": 0, "unit": "次"},
                {"name": "平均响应时间", "value": 0, "unit": "ms"},
                {"name": "缓存命中率", "value": 0, "unit": "%"}
            ]
            
            # 从指标收集器获取最新数据
            metrics = self.metrics_collector.get_metrics(limit=1)
            
            # 更新关键指标值
            for metric in metrics:
                if metric.name == "system.cpu.usage":
                    key_metrics[0]["value"] = round(metric.value, 1)
                elif metric.name == "system.memory.usage":
                    key_metrics[1]["value"] = round(metric.value, 1)
                elif metric.name == "system.disk.usage":
                    key_metrics[2]["value"] = round(metric.value, 1)
                elif metric.name == "app.requests.total":
                    key_metrics[3]["value"] = int(metric.value)
                elif metric.name == "app.errors.total":
                    key_metrics[4]["value"] = int(metric.value)
                elif metric.name == "app.response_time.avg":
                    key_metrics[5]["value"] = round(metric.value, 0)
                elif metric.name == "app.cache.hit_rate":
                    key_metrics[6]["value"] = round(metric.value, 1)
            
            return web.json_response(key_metrics)
            
        except Exception as e:
            self.logger.error(f"获取指标失败: {str(e)}")
            return web.json_response([])
    
    async def metric_history_handler(self, request):
        """指标历史处理器"""
        try:
            metric_name = request.match_info['metric_name']
            hours = int(request.query.get('hours', 24))
            
            history = self.metrics_collector.get_metric_history(metric_name, hours)
            return web.json_response(history)
            
        except Exception as e:
            self.logger.error(f"获取指标历史失败: {str(e)}")
            return web.json_response([])
    
    async def alerts_handler(self, request):
        """告警处理器"""
        try:
            level = request.query.get('level')
            acknowledged = request.query.get('acknowledged')
            resolved = request.query.get('resolved')
            limit = int(request.query.get('limit', 50))
            
            # 转换参数类型
            if acknowledged is not None:
                acknowledged = acknowledged.lower() == 'true'
            if resolved is not None:
                resolved = resolved.lower() == 'true'
            if level:
                level = AlertLevel(level)
            
            alerts = self.alert_manager.get_alerts(level, acknowledged, resolved, limit)
            
            # 转换为可序列化格式
            alerts_data = []
            for alert in alerts:
                alerts_data.append({
                    "rule_name": alert.rule_name,
                    "level": alert.level.value,
                    "message": alert.message,
                    "metric_name": alert.metric_name,
                    "current_value": alert.current_value,
                    "threshold": alert.threshold,
                    "timestamp": alert.timestamp.isoformat(),
                    "acknowledged": alert.acknowledged,
                    "resolved": alert.resolved,
                    "resolved_at": alert.resolved_at.isoformat() if alert.resolved_at else None
                })
            
            return web.json_response(alerts_data)
            
        except Exception as e:
            self.logger.error(f"获取告警失败: {str(e)}")
            return web.json_response([])
    
    async def acknowledge_alert_handler(self, request):
        """确认告警处理器"""
        try:
            alert_index = int(request.match_info['alert_index'])
            self.alert_manager.acknowledge_alert(alert_index)
            return web.json_response({"status": "success"})
            
        except Exception as e:
            self.logger.error(f"确认告警失败: {str(e)}")
            return web.json_response({"status": "error", "message": str(e)})
    
    async def resolve_alert_handler(self, request):
        """解决告警处理器"""
        try:
            alert_index = int(request.match_info['alert_index'])
            self.alert_manager.resolve_alert(alert_index)
            return web.json_response({"status": "success"})
            
        except Exception as e:
            self.logger.error(f"解决告警失败: {str(e)}")
            return web.json_response({"status": "error", "message": str(e)})
    
    async def system_status_handler(self, request):
        """系统状态处理器"""
        try:
            # 获取系统信息
            uptime = time.time() - psutil.boot_time()
            cpu_usage = psutil.cpu_percent(interval=1)
            memory_usage = psutil.virtual_memory().percent
            disk_usage = psutil.disk_usage('/').percent
            
            # 确定状态
            cpu_status = "status-ok" if cpu_usage < 70 else "status-warning" if cpu_usage < 85 else "status-error"
            memory_status = "status-ok" if memory_usage < 70 else "status-warning" if memory_usage < 85 else "status-error"
            disk_status = "status-ok" if disk_usage < 70 else "status-warning" if disk_usage < 85 else "status-error"
            
            status_data = {
                "uptime": f"{uptime // 3600:.0f}小时",
                "cpu_usage": round(cpu_usage, 1),
                "cpu_status": cpu_status,
                "memory_usage": round(memory_usage, 1),
                "memory_status": memory_status,
                "disk_usage": round(disk_usage, 1),
                "disk_status": disk_status
            }
            
            return web.json_response(status_data)
            
        except Exception as e:
            self.logger.error(f"获取系统状态失败: {str(e)}")
            return web.json_response({})
    
    async def start(self):
        """启动Dashboard"""
        if self.running:
            return
        
        try:
            self.runner = web.AppRunner(self.app)
            await self.runner.setup()
            
            self.site = web.TCPSite(self.runner, self.host, self.port)
            await self.site.start()
            
            self.running = True
            self.logger.info(f"监控Dashboard已启动: http://{self.host}:{self.port}")
            
        except Exception as e:
            self.logger.error(f"启动Dashboard失败: {str(e)}")
            raise MonitoringException(
                error_code=ErrorCode.MONITORING_ERROR,
                message="启动监控Dashboard失败"
            )
    
    async def stop(self):
        """停止Dashboard"""
        if not self.running:
            return
        
        try:
            if self.site:
                await self.site.stop()
            
            if self.runner:
                await self.runner.cleanup()
            
            self.running = False
            self.logger.info("监控Dashboard已停止")
            
        except Exception as e:
            self.logger.error(f"停止Dashboard失败: {str(e)}")


class PerformanceMonitor:
    """性能监控器"""
    
    def __init__(self, config: Config):
        self.config = config
        self.logger = Logger.get_logger(agent_name="performance_monitor")
        
        # 组件
        self.metrics_collector: Optional[MetricsCollector] = None
        self.alert_manager: Optional[AlertManager] = None
        self.dashboard: Optional[MonitoringDashboard] = None
        
        # 运行状态
        self.running = False
        
        self.logger.info("性能监控器初始化完成")
    
    async def start(self):
        """启动性能监控"""
        if self.running:
            return
        
        try:
            # 创建组件
            self.metrics_collector = MetricsCollector(self.config)
            self.alert_manager = AlertManager(self.config)
            self.dashboard = MonitoringDashboard(self.config, self.metrics_collector, self.alert_manager)
            
            # 启动组件
            await self.metrics_collector.start()
            await self.alert_manager.start()
            await self.dashboard.start()
            
            self.running = True
            self.logger.info("性能监控器已启动")
            
        except Exception as e:
            self.logger.error(f"启动性能监控器失败: {str(e)}")
            raise MonitoringException(
                error_code=ErrorCode.MONITORING_ERROR,
                message="启动性能监控器失败"
            )
    
    async def stop(self):
        """停止性能监控"""
        if not self.running:
            return
        
        try:
            # 停止组件
            if self.dashboard:
                await self.dashboard.stop()
            if self.alert_manager:
                await self.alert_manager.stop()
            if self.metrics_collector:
                await self.metrics_collector.stop()
            
            self.running = False
            self.logger.info("性能监控器已停止")
            
        except Exception as e:
            self.logger.error(f"停止性能监控器失败: {str(e)}")
    
    def record_metric(self, name: str, value: Union[int, float], 
                     metric_type: MetricType = MetricType.GAUGE,
                     labels: Dict[str, str] = None):
        """记录指标"""
        try:
            if self.metrics_collector:
                timestamp = datetime.now()
                self.metrics_collector._add_metric(name, metric_type, value, timestamp, 
                                                 {"labels": labels or {}})
        
        except Exception as e:
            self.logger.error(f"记录指标失败: {str(e)}")
    
    def add_alert_rule(self, rule: AlertRule):
        """添加告警规则"""
        try:
            if self.alert_manager:
                self.alert_manager.add_rule(rule)
        
        except Exception as e:
            self.logger.error(f"添加告警规则失败: {str(e)}")


# 全局监控器实例
_global_monitor: Optional[PerformanceMonitor] = None


async def get_performance_monitor() -> PerformanceMonitor:
    """获取全局性能监控器"""
    global _global_monitor
    
    if _global_monitor is None:
        config = Config()
        _global_monitor = PerformanceMonitor(config)
    
    return _global_monitor


async def get_metrics_collector() -> Optional[MetricsCollector]:
    """获取指标收集器"""
    monitor = await get_performance_monitor()
    return monitor.metrics_collector if monitor else None


async def get_alert_manager() -> Optional[AlertManager]:
    """获取告警管理器"""
    monitor = await get_performance_monitor()
    return monitor.alert_manager if monitor else None